<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kamar_model extends CI_Model{
	function __construct() {
		parent::__construct();
	}
	
	function add_kamar($clause){
		$this->db->insert('kamar',$clause);
//                die($this->db->last_query());
		return $this->db->insert_id();
	}
        
        function add_adjoining($clause){
		$this->db->insert('connecting_room',$clause);
//                die($this->db->last_query());
		return $this->db->insert_id();
	}
        
        function get_all_adjoining_by_kamar($id){
		$this->db->select("*,k1.nama AS nama_kamar1, k2.nama AS nama_kamar2");
                $this->db->join('kamar AS k1','id_room1=k1.id','left');
                $this->db->join('kamar AS k2','id_room2=k2.id','left');
                $this->db->where("id_room1 = ".$id." OR id_room2=".$id);
                $query = $this->db->get('connecting_room');
//                echo $this->db->last_query();
		return $query->result_array();
	}
        
        function get_all_id_adjoining_by_kamar($id){
                $this->db->where("id_room1 = ".$id." OR id_room2=".$id);
                $query = $this->db->get('connecting_room');
		return $query->result_array();
	}
        
     
        
        function delete_adjoining_by_kamar($id)
        {
            $this->db->where("id_room1 = ".$id." OR id_room2=".$id);
            $this->db->delete('connecting_room');
        }


        function update_kamar($data){
		$id = $data['id'];
		unset ($data['id']);
		$this->db->where('id',$id);
		$this->db->update('kamar',$data);
	}
	
	function get_kamar($id){
		$query = $this->db->get_where('kamar',array('id'=>$id));
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
        
        function get_kamar_join_kategori($id_kamar){
            $this->db->select("*,kamar.nama AS nama_kamar, kategori_kamar.nama AS kategori_kamar, kamar.id AS id_kamar");
                $this->db->join('kategori_kamar','kamar.id_kategori_kamar=kategori_kamar.id');
		$query = $this->db->get_where('kamar',array('kamar.id'=>$id_kamar));
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}
	
	function get_all_kamar(){
                $this->db->select("kamar.*, kategori_kamar.nama AS kategori_kamar");
                $this->db->join('kategori_kamar','kamar.id_kategori_kamar=kategori_kamar.id');
		$query = $this->db->get('kamar');
		return $query->result_array();
	}
        
        function get_all_kamar_join_kategori(){
            $this->db->select("*,kamar.nama AS nama_kamar, kategori_kamar.nama AS kategori_kamar, kamar.id AS id_kamar");
                $this->db->join('kategori_kamar','kamar.id_kategori_kamar=kategori_kamar.id');
		$query = $this->db->get('kamar');
		return $query->result_array();
	}
        
        function get_kamar_clause($clause)
        {
            $query = $this->db->get_where('kamar',$clause);
		if($query->num_rows>0){
			return $query->row_array();
		}else{
			return FALSE;
		}
        }


        function delete_kamar($id)
        {
            $this->db->delete('kamar',array('id' => $id));
        }
}

/** End of file */